import os

import requests
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget

import global_config
from ui.Set_new_password_window import Set_new_password_window
from window.My_pop_up_warning import My_pop_up_warning


class My_set_new_password_window(QWidget, Set_new_password_window):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        # 设置界面标题、图标等
        self.setWindowTitle('忘记密码')
        base_path = os.path.dirname(__file__)
        base_path = base_path.replace('\\window', '')
        icon_path = os.path.join(base_path, 'icon')
        icon_path = os.path.join(icon_path, 'Python.png')
        icon = QIcon(icon_path)
        self.setWindowIcon(icon)

        self.input_password.setPlaceholderText('请输入新密码：')
        self.input_confirm_password.setPlaceholderText('请确认新密码：')

        self.pop_up_warning = My_pop_up_warning()

        self.username = ''

        # 设置信号与槽
        self.input_password.textChanged.connect(self.update_input_password_warning)
        self.input_confirm_password.textChanged.connect(self.update_input_confirm_password_warning)
        self.cancel_button.clicked.connect(self.cancel)
        self.confirm_button.clicked.connect(self.confirm)

    def set_username(self, username):
        self.username = username

    def update_input_password_warning(self):
        if self.input_password.text() == '':
            self.input_password_warning.setText('密码不能为空')
        elif self.check_password(self.input_password.text()):
            self.input_password_warning.setText('密码应包含大小写字母和数字')
        else:
            self.input_password_warning.setText('')

    def update_input_confirm_password_warning(self):
        if self.input_confirm_password.text() == '':
            self.input_confirm_password_warning.setText('密码不能为空')
        elif self.input_confirm_password.text() != self.input_password.text():
            self.input_confirm_password_warning.setText('两次密码输入不一致')
        else:
            self.input_confirm_password_warning.setText('')

    def cancel(self):
        self.close()
        self.input_password.setText('')
        self.input_confirm_password.setText('')
        self.input_password_warning.setText('')
        self.input_confirm_password_warning.setText('')

    def confirm(self):
        is_empty = False
        is_error = False
        if self.input_password.text() == '' or self.input_confirm_password.text() == '':
            is_empty = True
        if self.check_password(self.input_password.text()) or self.check_password(self.input_confirm_password.text()):
            is_error = True
        if not is_empty and not is_error:
            url = 'http://' + global_config.localhost + ':8000/set_password/'
            params = {
                'username': self.username,
                'password': self.input_password.text(),
                'password_confirm': self.input_confirm_password.text()
            }
            result = requests.get(url, params).json()
            if result['result'] == 'success':
                self.pop_up_warning.warning_info_label.setText('设置成功')
                self.pop_up_warning.warning_info_text.setText('请重新登录')
                self.pop_up_warning.show()
                self.hide()
                self.input_password.setText('')
                self.input_confirm_password.setText('')
                self.input_password_warning.setText('')
                self.input_confirm_password_warning.setText('')
            else:
                self.pop_up_warning.warning_info_label.setText('设置失败')
                self.pop_up_warning.warning_info_text.setText('请再试一次')
                self.pop_up_warning.show()

    def check_password(self, password):
        result1 = False
        result2 = False
        result3 = False
        for i in password:
            if '0' <= i <= '9':
                result1 = True
            elif 'a' <= i <= 'z':
                result2 = True
            elif 'A' <= i <= 'Z':
                result3 = True
        return not (result1 and result2 and result3)
